package com.wy.allthingdone.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.graphics.Color;

import com.wy.allthingdone.R;
import com.wy.allthingdone.views.GifView;
import com.wy.allthingdone.views.NewTextView;

import java.util.List;

/**
 * Created by 38270 on 2016/11/1.
 */

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHodler> {
    private List<String> list = null;
    private Context context;
    private ItemOnClickEvent itemOnClickEvent;

    public void setItemOnClickEvent(ItemOnClickEvent itemOnClickEvent) {
        this.itemOnClickEvent = itemOnClickEvent;
    }

    public void setStopPlay(Boolean stopPlay) {
        this.stopPlay = stopPlay;
    }

    private Boolean stopPlay = false;

    public ItemAdapter(Context context, List<String> list) {
        this.list = list;
        this.context = context;
    }

    @Override
    public ItemViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        //设置初始化ViewHolder
        Log.i("RV", "onCreateViewHolder================创建");
        return new ItemViewHodler(LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(ItemViewHodler holder, int position) {//绑定holder
        Log.i("RV", "onBindViewHolder================设置");
        final int item_position = position;
        if (list.get(position).contains(".")) {
            if (list.get(position).contains(".apk")) {
                holder.getGv().setImageResource(R.drawable.ic_launcher);
            } else if (list.get(position).contains(".png") || list.get(position).contains(".PNG")) {
                holder.getGv().setImageResource(R.drawable.file_png);
            } else if (list.get(position).contains(".MP4") || list.get(position).contains(".mp4")) {
                holder.getGv().setImageResource(R.drawable.file_mp4);
            } else if (list.get(position).contains(".MP3") || list.get(position).contains(".mp3")) {
                holder.getGv().setImageResource(R.drawable.file_mp3);
            } else if (list.get(position).contains(".JPG") || list.get(position).contains(".jpg")) {
                holder.getGv().setImageResource(R.drawable.file_jpg);
            } else if (list.get(position).contains(".ttf") || list.get(position).contains(".TTF")) {
                holder.getGv().setImageResource(R.drawable.file_tiff);
            } else if (list.get(position).contains(".txt") || list.get(position).contains(".TXT")) {
                holder.getGv().setImageResource(R.drawable.file_txt);
            } else {
                holder.getGv().setImageResource(R.drawable.file_ini);
            }
            holder.getText().setText(list.get(position), TextView.BufferType.NORMAL, "100");
            ((View) holder.getText().getParent()).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemOnClickEvent != null) {
                        itemOnClickEvent.doItemClick(list.get(item_position));
                    }
                }
            });
        } else {
            holder.getGv().setImageResource(R.drawable.file_directory);
            holder.getText().setText(list.get(position), TextView.BufferType.NORMAL, "100");
            ((View) holder.getText().getParent()).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemOnClickEvent != null) {
                        itemOnClickEvent.doItemClick(list.get(item_position));
                    }
                }
            });
        }
        holder.getText().setTextColor(Color.parseColor("#FF000000"));
        holder.getText().setText(list.get(position).substring(list.get(position).lastIndexOf("/") + 1), TextView.BufferType.NORMAL, "100");


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ItemViewHodler extends RecyclerView.ViewHolder {
        GifView gv;

        NewTextView text;

        public GifView getGv() {
            return gv;
        }

        public NewTextView getText() {
            return text;
        }

        public ItemViewHodler(View itemView) {
            super(itemView);
            gv = (GifView) itemView.findViewById(R.id.item_gifview);
            text = (NewTextView) itemView.findViewById(R.id.text);
        }
    }

    public interface ItemOnClickEvent {
        void doItemClick(String where);
    }

    public void setAdapterNoti(List<String> list) {
        this.list = list;
        notifyDataSetChanged();
    }
}
